package com.moyun.zbmy.zizhou.ui.activities;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Pair;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gridsum.tracker.GridsumWebDissector;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.moyun.zbmy.zizhou.R;
import com.moyun.zbmy.zizhou.application.Cache;
import com.moyun.zbmy.zizhou.application.Constances;
import com.moyun.zbmy.zizhou.base.BaseActivity;
import com.moyun.zbmy.zizhou.beans.LiveItem;
import com.moyun.zbmy.zizhou.beans.NewsItem;
import com.moyun.zbmy.zizhou.beans.SearchDataBean;
import com.moyun.zbmy.zizhou.beans.SearchKeyWords;
import com.moyun.zbmy.zizhou.beans.SearchResult;
import com.moyun.zbmy.zizhou.beans.TopicItem;
import com.moyun.zbmy.zizhou.http.AbsNetCallBack;
import com.moyun.zbmy.zizhou.http.NetUtils;
import com.moyun.zbmy.zizhou.tts.SpeachUtils;
import com.moyun.zbmy.zizhou.ui.adapter.SearchNewsAdapter;
import com.moyun.zbmy.zizhou.ui.adapter.holder.VideoPlayHolder;
import com.moyun.zbmy.zizhou.ui.util.IListShowData;
import com.moyun.zbmy.zizhou.ui.utils.KeyBoardUtils;
import com.moyun.zbmy.zizhou.ui.utils.SkipUtil;
import com.moyun.zbmy.zizhou.ui.widget.MyStellarMap;
import com.moyun.zbmy.zizhou.ui.widget.VoiceLongPressView;
import com.shitou.googleplay.lib.randomlayout.StellarMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 报料语音输入
 */
public class VoiceInputActivity extends BaseActivity implements SpeachUtils.OnVoiceRecordListener {
    @BindView(R.id.status_txt)
    protected CustomFontTextView statusTxt;
    @BindView(R.id.voice_bg)
    protected ImageView voiceBg;
    @BindView(R.id.voice_input)
    protected VoiceLongPressView voiceLongPress;
    private SpeachUtils speachUtils;
    private AnimationDrawable voiceIconAnimation;

    public static final String VOICE_INPUT = "voice_input";
    private boolean isAddMore = false;
    private int count = 0;
    private Handler handler = new Handler();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initVoiceLongPress();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_voice_input);
        ButterKnife.bind(this);
    }

    private void initView() {
        statusTxt.setText("长按说话");
    }

    private void initVoiceLongPress() {
        voiceLongPress.setLongClickListener(new VoiceLongPressView.OnLongVoiceClickListener() {
            @Override
            public void onLongClick() {
                //获取 AnimationDrawable
                voiceIconAnimation = (AnimationDrawable) voiceBg.getDrawable();
                voiceIconAnimation.start();
                speachUtils = new SpeachUtils(VoiceInputActivity.this);
                speachUtils.setVoiceRecordListener(VoiceInputActivity.this);
                JLog.e("VoiceLongPressView --- ", "OnPressDown");
                voiceLongPress.setImageResource(R.mipmap.search_voice_press);
                statusTxt.setText("松开完成");
                speachUtils.startRecord();
            }

            @Override
            public void onClickUp() {
                voiceIconAnimation = (AnimationDrawable) voiceBg.getDrawable();
                voiceIconAnimation.stop();
                voiceIconAnimation.selectDrawable(0);
                voiceLongPress.setImageResource(R.mipmap.search_voice);
                statusTxt.setText("长按说话");
                speachUtils.stopRecord();
            }
        });
    }

    private String inputTxt = "";

    @Override
    public void OnRecordBack(String txt) {
        if (txt.endsWith("。") || txt.endsWith("！") || txt.endsWith("？")) {
            inputTxt += txt;
            int length = inputTxt.length();
            if (TextUtils.isEmpty(inputTxt) || length <= 2) {
                toast("输入内容无效，请重新输入");
            } else {
                Intent intent = new Intent();
                intent.putExtra(VOICE_INPUT, inputTxt);
                setResult(RESULT_OK, intent);
                finish();
            }
        }else{
            inputTxt += txt;
        }
    }

    @OnClick(value = {R.id.voice_input, R.id.btn_cancel})
    public void speakClick(View view) {
        switch (view.getId()) {
            case R.id.voice_input:

                break;
            case R.id.btn_cancel:
                finish();
                break;
        }
    }


}
